package com.yoki.cloud.common.protocol.radius.attribute.attr.check;

import com.yoki.cloud.common.protocol.radius.attribute.attr.RadiusMsgAttr;
import com.yoki.cloud.common.utils.encrypt.EncryptUtils;
import lombok.Getter;
import lombok.Setter;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/1/15$ 14:54$
 */
public class UserPasswordAttr extends RadiusMsgAttr<String> {

    public static final int ATTR_TYPE = 2;

    @Getter
    @Setter
    public String secret;

    public byte[] requestAuthenticator;

    public UserPasswordAttr(String secret, byte[] requestAuthenticator) {
        this.secret = secret;
        this.requestAuthenticator = requestAuthenticator;
    }

    @Override
    public int getAttrType() {
        return ATTR_TYPE;
    }

    @Override
    protected String parseValue(byte[] input) {
        String value = null;
        try {
            value = EncryptUtils.decryptPAP(input, this.secret.getBytes(), this.requestAuthenticator);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    public byte[] getBytes(String newSecret) {
        String oldSecret = secret;
        secret = newSecret;
        byte[] bytes = getBytes();
        secret = oldSecret;
        return bytes;
    }

    @Override
    protected byte[] getValueBytes() {
        return EncryptUtils.encryptPAP(this.value.getBytes(), this.secret.getBytes(), this.requestAuthenticator);
    }

}
